//
//  LGBottomAlertView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/19.
//  底部选项弹框

#import "LGBottomAlertView.h"

@interface LGBottomAlertView()
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;

@end

@implementation LGBottomAlertView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonTouched)]];
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        [self addSubview:self.baseView];
    }
    return self;
}

-(void)itemButtonTouched:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    if(self.itemButtonTouched){
        self.itemButtonTouched(index);
    }
    [self cancelButtonTouched];
}

-(void)cancelButtonTouched{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, self.baseView.bounds.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
    CGFloat topMargin = viewPix(2);
    for (NSInteger i=0; i<titleArray.count; i++) {
        UIButton *button = [UIButton buttonWithTitle:titleArray[i] titleFont:LGFont(16) textColor:@"#2B2A37" imageName:@""];
        button.frame = CGRectMake(0, topMargin, Screen_W, viewPix(58));
        button.tag = 1000+i;
        [button addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
        [self.baseView addSubview:button];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, kMaxY(button.frame), Screen_W, 0.5)];
        lineView.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
        [self.baseView addSubview:lineView];
        topMargin += viewPix(58);
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, topMargin, Screen_W, viewPix(10))];
    lineView.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
    [self.baseView addSubview:lineView];
    
    UIButton *cancelBtn = [UIButton buttonWithTitle:@"取消" titleFont:LGFont(16) textColor:@"#2B2A37" imageName:@""];
    cancelBtn.frame = CGRectMake(0, kMaxY(lineView.frame), Screen_W, viewPix(52));
    [cancelBtn addTarget:self action:@selector(cancelButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.baseView addSubview:cancelBtn];
    CGFloat height = kMaxY(cancelBtn.frame);
    self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, height+viewPix(20));
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, Screen_H-height-bottomSafeBarHeight/3.0, Screen_W, height+viewPix(20)+bottomSafeBarHeight/3.0);
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(100))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

@end
